package test;

import org.eclipse.swt.SWT;
import org.eclipse.swt.dnd.DND;
import org.eclipse.swt.dnd.DragSource;
import org.eclipse.swt.dnd.DragSourceAdapter;
import org.eclipse.swt.dnd.DragSourceEvent;
import org.eclipse.swt.dnd.DropTarget;
import org.eclipse.swt.dnd.DropTargetAdapter;
import org.eclipse.swt.dnd.DropTargetEvent;
import org.eclipse.swt.dnd.TextTransfer;
import org.eclipse.swt.dnd.Transfer;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Tree;
import org.eclipse.swt.widgets.TreeItem;

public class Dnd1 {

	public Dnd1() {
		super();
		// TODO Auto-generated constructor stub
	}

	public static void main(String[] args) {
		Display display = new Display();
		Shell shell = new Shell(display);
		final Tree tree = new Tree(shell, SWT.BORDER);
		for (int i = 2000; i <= 2005; i++) {
			TreeItem item1 = new TreeItem(tree, SWT.NONE);
			item1.setText("year " + i);
		}
		tree.pack();
		DragSource ds = new DragSource(tree, DND.DROP_MOVE);
		ds.setTransfer(new Transfer[] { TextTransfer.getInstance() });
		ds.addDragListener(new DragSourceAdapter() {
			public void dragStart(DragSourceEvent event) {

			}

			public void dragSetData(DragSourceEvent event) {
				event.data = tree.getSelection()[0].getText();
			}
		});

		final Label label = new Label(shell, SWT.BORDER);
		label.setBounds(100, 50, 50, 50);
		DropTarget dr = new DropTarget(label, DND.DROP_MOVE);
		dr.setTransfer(new Transfer[] { TextTransfer.getInstance() });
		dr.addDropListener(new DropTargetAdapter() {
			public void dragEnter(DropTargetEvent event) {
				System.out.print((String) event.data);
			}

			public void dropAccept(DropTargetEvent event) {

			}

			public void drop(DropTargetEvent event) {
				label.setText((String) event.data);
			}
		});

		shell.open();
		while (!shell.isDisposed())
			if (!display.readAndDispatch())
				display.sleep();
		display.dispose();
	}

}
